package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.igas.bomp.facade.BompMchntQueryService;
import cc.rengu.igas.bomp.facade.bean.SignImageInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntSignInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntSignInfoQueryResponse;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.redp.bizimpl.enums.BompQueryTypeEnum;
import cc.rengu.redp.bizimpl.enums.BompTxnNumEnum;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.dao.UpmpBMchntSignInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpBMchntSignInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpBMchntSignInfoService;
import cc.rengu.redp.bizimpl.upmp.vo.ImageInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.exception.RedpException;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-22
 */
@Service
public class UpmpBMchntSignInfoServiceImpl extends ServiceImpl<UpmpBMchntSignInfoMapper, UpmpBMchntSignInfo> implements IUpmpBMchntSignInfoService {


    @Override
    public MchntSignInfoQueryResponse mchntSignInfoQuery(String instId, MchntSignInfoQueryRequest req, BompMchntQueryService bompQueryService, String traceNo, BompQueryTypeEnum queryType)throws RedpException {

        req.setHeader(CommonUtils.packageBompHeader(traceNo, BompTxnNumEnum.mchntSignInfoQuery.getType(),instId,queryType.getType()));

        try{
            log.trace("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+BompTxnNumEnum.mchntSignInfoQuery.getDesc()+"交易请求报文：" + JSON.toJSONString(req));

            Result<MchntSignInfoQueryResponse> response= bompQueryService.mchntSignInfoQuery(req);
            log.trace("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+BompTxnNumEnum.mchntSignInfoQuery.getDesc()+"交易返回报文：" + JSON.toJSONString(response));
            if(response.isSuccess()){
                MchntSignInfoQueryResponse resp = response.getResult();
                // 返回码：00-进件成功，其他均视为失败
                if (BusinessContainer.BSPS_SUCCESS.equals(resp.getRespCode())) {
                    log.trace("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+BompTxnNumEnum.mchntSignInfoQuery.getDesc()+"交易成功");
                    return resp;
                }else {
                    log.error("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+BompTxnNumEnum.mchntSignInfoQuery.getDesc()+"交易失败");
                    return null;
                }
            }else{
                log.error("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+BompTxnNumEnum.mchntSignInfoQuery.getDesc()+"交易没有获得返回信息");
                return null;
            }
        }catch (Exception e) {
            // TODO: handle exception
            log.error("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+BompTxnNumEnum.mchntSignInfoQuery.getDesc()+"交易出现异常，异常信息："+e.getMessage(),e);
            throw new RedpException("法人行["+instId+"],用户标识[" + req.getUserId() + "],商户号["+req.getMchntNo()+"]"+BompTxnNumEnum.mchntSignInfoQuery.getDesc()+"交易出现异常，异常信息："+e.getMessage());
        }
    }


    @Override
    public void buildBMchntSign(MchntSignInfoQueryResponse entityDB, UpmpBMchntSignInfo entity){
        if(Common.isNotBlank(entityDB)&&Common.isNotBlank(entity)){
            BeanUtils.copyProperties(entityDB,entity);
            if(Common.isNotBlank(entityDB.getMchntArtifInfo())){
                BeanUtils.copyProperties(entityDB.getMchntArtifInfo(),entity);
                if(Common.isNotBlank(entityDB.getMchntArtifInfo().getCertFaceImage())){
                    SignImageInfoBean dbImage = entityDB.getMchntArtifInfo().getCertFaceImage();
                    ImageInfoVO img = new ImageInfoVO();
                    BeanUtils.copyProperties(dbImage,img);
                    entity.setCertFaceImage(img);
                }
                if(Common.isNotBlank(entityDB.getMchntArtifInfo().getCertBackImage())){
                    SignImageInfoBean dbImage = entityDB.getMchntArtifInfo().getCertBackImage();
                    ImageInfoVO img = new ImageInfoVO();
                    BeanUtils.copyProperties(dbImage,img);
                    entity.setCertBackImage(img);
                }
            }
            if(Common.isNotBlank(entityDB.getBusinessLicenceInfo())){
                BeanUtils.copyProperties(entityDB.getBusinessLicenceInfo(),entity);

                if(Common.isNotBlank(entityDB.getBusinessLicenceInfo().getSignImageInfoBean())){
                    SignImageInfoBean dbImage = entityDB.getBusinessLicenceInfo().getSignImageInfoBean();
                    ImageInfoVO img = new ImageInfoVO();
                    BeanUtils.copyProperties(dbImage,img);
                    entity.setLicenceImage(img);
                }
            }
        }
    }
}
